package com.wjk.kylin.rpc.core.config;

import com.wjk.kylin.rpc.core.provider.impl.RpcSpringProviderFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author wjk
 */
@Configuration
@EnableConfigurationProperties({RpcProviderConfigProperties.class})
public class RpcProviderConfig {
    private static final Logger LOGGER = LoggerFactory.getLogger(RpcProviderConfig.class);

    @Autowired
    private RpcProviderConfigProperties properties;


    /**
     * ConditionalOnProperty
     * name和value不能同时存在.也不能同时不存在. 两者只能存在一个
     * 如果havingValue存在, 则跟havingValue的值进行比较, 相同返回true, 不同返回false
     * 如果没有指定havingValue, 则用prefix + name 或者prefix + value获取配置项的值, 然后跟 "false"字符串比较, 相同返回false, 不同返回true
     * 相同: 即配置项值为false时, 例journal.jnc.enabled=false, 此时是不加载被修饰的类或方法
     * 不同: 即配置项值为非false的任何值, 例journa.jnc.enabled=true / journa.jnc.enabled=123 / journa.jnc.enabled= 等, 都是返回true. 此时加载被修饰的类或方法
     * 在配置matchIfMissing后, 如果prefix + name 或者prefix + value 都不存在, 则以matchIfMissing的值为准.
     * matchIfMissing = true, 则加载
     * matchIfMissing = false, 则不加载
     * @return
     */
    @Bean
    @ConditionalOnMissingBean(RpcSpringProviderFactory.class)
    @ConditionalOnProperty(name = "kylin.rpc.provider.enable", havingValue = "true")
    public RpcSpringProviderFactory springProviderFactory() {

        RpcSpringProviderFactory providerFactory = new RpcSpringProviderFactory();

        providerFactory.setProperties(properties);

        LOGGER.info(">>>>>>>>>>> kylin-rpc provider config init finish");
        return providerFactory;
    }
}
